<template>
  <!-- 搜索工作栏 -->
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams">
      <!-- 新增等操作按钮 -->
      <template #actionMore>
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          plain-->
        <!--          @click="openForm('create')"-->
        <!--          v-hasPermi="['digital:cardkey-product:create']"-->
        <!--        >-->
        <!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
        <!--        </el-button>-->
        <el-button
          type="primary"
          plain
          style="position:relative;"
          :loading="importLoading"
          v-hasPermi="['digital:cardkey-product:import']"
        >
          <input class="upload-file-input" @change="onFileChange" type="file"/>
          <Icon icon="ep:plus" class="mr-5px"/>
          导入
        </el-button>
        <el-button
          type="primary"
          plain
          style="position:relative;"
          :loading="exportLoading"
          @click="exportData"
          v-hasPermi="['digital:cardkey-product:export']"
        >
          导出
        </el-button>
      </template>
    </Search>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <Table
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
    >
<!--      <template #source="{row}">-->
<!--        {{EnumSourceTypeText[row.source] || "&#45;&#45;"}}-->
<!--      </template>-->
      <template #denominationFixed="{row}">
        {{row.denominationFixed / 100 || 0}}
      </template>
      <template #denominationMin="{row}">
        {{row.denominationMin / 100 || 0}}
      </template>
      <template #denominationMax="{row}">
        {{row.denominationMax / 100 || 0}}
      </template>
<!--      <template #action="{ row }">-->
<!--        <el-button-->
<!--          link-->
<!--          type="primary"-->
<!--          @click="openForm('update', row.id)"-->
<!--          v-hasPermi="['digital:cardkey-product:update']"-->
<!--        >-->
<!--          编辑-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          link-->
<!--          type="danger"-->
<!--          v-hasPermi="['digital:cardkey-product:delete']"-->
<!--          @click="handleDelete(row.id)"-->
<!--        >-->
<!--          删除-->
<!--        </el-button>-->
<!--      </template>-->
    </Table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CardkeyProductForm ref="formRef" @success="getList"/>
</template>
<script setup lang="ts" name="CardkeyProduct">
import {allSchemas, EnumSourceTypeText} from './cardkeyProduct.data'
import * as CardkeyProductApi from '@/api/digital/cardkeyproduct'
import CardkeyProductForm from './CardkeyProductForm.vue'
import {downloadByData} from "@/utils/filt";

const message = useMessage() // 消息弹窗
const importLoading = ref(false);
const exportLoading = ref(false);
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
// 详细可见：https://doc.iocoder.cn/vue3/crud-schema/
const {tableObject, tableMethods} = useTable({
  getListApi: CardkeyProductApi.getCardkeyProductPage, // 分页接口
  delListApi: CardkeyProductApi.deleteCardkeyProduct // 删除接口
})
// 获得表格的各种操作
const {getList, setSearchParams} = tableMethods

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = (id: number) => {
  tableMethods.delList(id, false)
}

const exportData = ()=> {
  exportLoading.value = true;
  CardkeyProductApi.exportCardkeyProductApi({
    pageNo: tableObject.currentPage,
    pageSize: tableObject.pageSize
  }).then((res)=> {
    downloadByData(res, "CDK数据.xlsx")
    console.log(res);
  }).finally(e=> {
    exportLoading.value = false;
  })
}

const onFileChange = (event) => {
  const file = event.currentTarget.files[0];

  if (!file) {
    return message.warning("请选择文件");
  }

  let fileName = file.name;
  // 获取文件的文件后缀
  let splitList = fileName.split(".");
  let extension = splitList[splitList.length - 1];
  if (extension.toLowerCase() !== "xlsx") {
    return message.warning("文件格式错误");
  }

  importLoading.value = true;
  let params = new FormData()
  params.append('file', file)
  CardkeyProductApi.importCardkeyProductApi(params).then(() => {
    message.success("导入成功");
    getList();
  }).finally(e => {
    importLoading.value = false;
  });
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.upload-file-input {
  width: 100%;
  height: 100%;
  opacity: 0;
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: 1000;
}

</style >
